package com.snack.shop.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.snack.shop.common.Result;
import com.snack.shop.entity.Message;
import com.snack.shop.service.MessageService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@Tag(name = "消息中心")
@RestController
@RequestMapping("/message")
@RequiredArgsConstructor
public class MessageController {

    private final MessageService messageService;

    @Operation(summary = "分页查询我的消息")
    @GetMapping("/page")
    public Result<Page<Message>> page(
            @Parameter(description = "类型：1订单 2反馈 3系统") @RequestParam(required = false) Integer type,
            @Parameter(description = "当前页") @RequestParam(defaultValue = "1") Integer pageNum,
            @Parameter(description = "每页大小") @RequestParam(defaultValue = "10") Integer pageSize
    ) {
        Long userId = StpUtil.getLoginIdAsLong();
        Page<Message> page = messageService.pageByUser(userId, type, pageNum, pageSize);
        return Result.success(page);
    }

    @Operation(summary = "未读数量")
    @GetMapping("/unread-count")
    public Result<Integer> unreadCount() {
        Long userId = StpUtil.getLoginIdAsLong();
        return Result.success(messageService.getUnreadCount(userId));
    }

    @Operation(summary = "标记已读")
    @PostMapping("/{id}/read")
    public Result<Void> markRead(@PathVariable Long id) {
        Long userId = StpUtil.getLoginIdAsLong();
        messageService.markRead(userId, id);
        return Result.success();
    }

    @Operation(summary = "全部已读")
    @PostMapping("/read-all")
    public Result<Void> markAllRead() {
        Long userId = StpUtil.getLoginIdAsLong();
        messageService.markAllRead(userId);
        return Result.success();
    }
}
